﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TheQuest
{
    public partial class Form1 : Form
    {
        private Game game;
        private Random random = new Random();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            game = new Game(new Rectangle(78, 57, 420, 155));
            game.NewLevel(random);
            UpdateCharacters();
        }

        ///<summary>
        ///The following four button events will call the game object's move method.
        ///It passes with it a direction the player is moving from the directions enum,
        ///as well as a random reference for the use of the game's move method passing of the enemies' move method.
        ///It then updates the locations of the characters.
        ///</summary>
        private void moveUpButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Up, random);
            UpdateCharacters();
        }

        private void moveDownButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Down, random);
            UpdateCharacters();
        }

        private void moveLeftButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Left, random);
            UpdateCharacters();
        }

        private void moveRightButton_Click(object sender, EventArgs e)
        {
            game.Move(Direction.Right, random);
            UpdateCharacters();
        }

        /// <summary>
        /// The following four button events will call the game objects attack method.
        /// It passes the direction the player is attacking from the direction enum,
        /// as well as a random reference for the use of the game's attack method passing of the player's attack method.
        /// It then updates the location of the characters. It will also check the player's inventory and make sure the correct
        /// icons are displayed on the inventory scroll.
        /// </summary>

        private void attackUpButton_Click(object sender, EventArgs e)
        {
            game.Attack(Direction.Up, random);
            UpdateCharacters();
        }

        private void attackDownButton_Click(object sender, EventArgs e)
        {
            game.Attack(Direction.Down, random);
            UpdateCharacters();
        }

        private void attackLeftButton_Click(object sender, EventArgs e)
        {
            game.Attack(Direction.Left, random);
            UpdateCharacters();
        }

        private void attackRightButton_Click(object sender, EventArgs e)
        {
            game.Attack(Direction.Right, random);
            UpdateCharacters();
        }

        
        /// <summary>
        /// The following method reads the location of the players, enemies, 
        /// and any weapons currently in the dungeon and moves the PictureBoxes to match them.
        /// </summary>
        public void UpdateCharacters()
        {
            player.Location = game.PlayerLocation;
            playerHitPointsLabel.Text = game.PlayerHitPoints.ToString();

            bool showBat = false;
            bool showGhost = false;
            bool showGhoul = false;
            int enemiesShown = 0;
            foreach (Enemy enemy in game.Enemies)
            {
                if (enemy is Bat)
                {
                    batHitPointsLabel.Location = enemy.Location;
                    batHitPointsLabel.Text = enemy.HitPoints.ToString();
                    if (enemy.HitPoints > 0)
                    {
                        showBat = true;
                        enemiesShown++;
                    }
                }

                if (enemy is Ghost)
                {
                    batHitPointsLabel.Location = enemy.Location;
                    batHitPointsLabel.Text = enemy.HitPoints.ToString();
                    if (enemy.HitPoints > 0)
                    {
                        showGhost = true;
                        enemiesShown++;
                    }
                }

                if (enemy is Ghoul)
                {
                    batHitPointsLabel.Location = enemy.Location;
                    batHitPointsLabel.Text = enemy.HitPoints.ToString();
                    if (enemy.HitPoints > 0)
                    {
                        showGhoul = true;
                        enemiesShown++;
                    }
                }
            }

            sword.Visible = false;
            bow.Visible = false;
            redPotion.Visible = false;
            bluePotion.Visible = false;
            mace.Visible = false;
            Control weaponControl = null;
            switch (game.WeaponInRoom.Name)
            {
                case "Sword":
                    weaponControl = sword; break;
                case "Bow":
                    weaponControl = bow; break;
                case "Red Potion":
                    weaponControl = redPotion; break;
                case "Blue Potion":
                    weaponControl = bluePotion; break;
                case "Mace":
                    weaponControl = mace; break;
            }
            weaponControl.Visible = true;
            
            //Set the visible property on each inventory icon PictureBox.

            ///foreach (Weapon weapon in game.PlayerWeapons)
            ///{
            ///if (the players weapons object exists)
            ///visibility property for that weapon = false;
            ///}

            foreach (Weapon weapon in game.PlayerWeapons)
            {
                if (game.PlayerWeapons.Contains(weapon.Name))
                {
                    weapon.visible = false;
                }
            }

            weaponControl.Location = game.WeaponInRoom.Location;
            if (game.WeaponInRoom.PickedUp)
            {
                weaponControl.Visible = false;
            }
            else
            {
                weaponControl.Visible = true;
            }
            if (game.PlayerHitPoints <= 0)
            {
                MessageBox.Show("You died, rather unfortunate. :(");
                Application.Exit();
            }
            if (enemiesShown < 1)
            {
                MessageBox.Show("You have defeated the enemies on this level!");
                game.NewLevel(random);
                UpdateCharacters();
            }
            
        }

        private void inventorySwordPictureBox_Click(object sender, EventArgs e)
        {
            if (game.CheckPlayerInventory("Sword"))
                game.Equip("Sword");
            inventorySwordPictureBox.BorderStyle = BorderStyle.FixedSingle;
        }

        private void inventoryBluePotionPictureBox_Click(object sender, EventArgs e)
        {
            if (game.CheckPlayerInventory("Blue Potion"))
                game.Equip("Blue Potion");
            inventoryBluePotionPictureBox.BorderStyle = BorderStyle.FixedSingle;
        }

        private void inventoryBowPictureBox_Click(object sender, EventArgs e)
        {
            if (game.CheckPlayerInventory("Bow"))
                game.Equip("Bow");
            inventoryBowPictureBox.BorderStyle = BorderStyle.FixedSingle;
        }

        private void inventoryRedPotionPictureBox_Click(object sender, EventArgs e)
        {
            if (game.CheckPlayerInventory("Red Potion"))
                game.Equip("Red Potion");
            inventoryRedPotionPictureBox.BorderStyle = BorderStyle.FixedSingle;
        }

        private void inventoryMacePictureBox_Click(object sender, EventArgs e)
        {
            if (game.CheckPlayerInventory("Mace"))
                game.Equip("Mace");
            inventoryMacePictureBox.BorderStyle = BorderStyle.FixedSingle;
        }
    }
}
